/*
 * MorseEncoder.cpp
 *
 *  Created on: Jun 30, 2012
 *      Author: nam20485
 */

#include "MorseEncoder.h"
#include "LinkedList.h"

MorseEncoder::MorseEncoder(const String& strMessage)
	: _aEncoding(NULL),
	_strMessage(strMessage)
{
	// called by Encoding(), so it is evaluated only as-needed
	//Encode();
}

MorseEncoder::~MorseEncoder()
{
	if (_aEncoding)
	{
		// delete [] does not appear to be defined
		//delete [] _aEncoding;
		delete _aEncoding;
		_aEncoding = NULL;
	}
}

const Morse::SYMBOL* MorseEncoder::Encoding()
{
	if (_aEncoding == NULL)
	{
		_aEncoding = Encode(_strMessage);
	}

	return _aEncoding;
}


const Morse::SYMBOL* MorseEncoder::Encode(const String& str)
{
	const Morse::SYMBOL* aEncoding = NULL;

	LinkedList<const Morse::SYMBOL> symbols;

	for (unsigned int i = 0; i < str.length(); i++)
	{
		char curr = str[i];
		if (curr == ' ')	// space, end of a word
		{
			symbols.Add(Morse::WORD_BREAK);
		}
		else	// some letter, not space
		{
			//const Morse::SYMBOL* aLetter = Encode(curr);
			// append to symbols

			char next = 0;
			if (i+1 < str.length())
			{
				next = str[i+1];
			}

			if (next != ' ' && next != 0)
			{
				symbols.Add(Morse::LETTER_BREAK);
			}
			// else if (next == ' ')	// last character of word, do nothing, next space will add _WORD_BREAK
			// else if (next == 0)		// last character of string, do nothing loop will break and EOC will be added
		}
	}

	symbols.Add(Morse::EOC);

	// walk symbols list and add to aEncoding

	return aEncoding;
}

const Morse::SYMBOL* MorseEncoder::Encode(const char c)
{
	const Morse::SYMBOL* aEncoding = NULL;
	return aEncoding;
}
